package com.bes.bessdk.scan;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

public class BtHeleper {

    private static final String TAG = "BtHeleper";

    public static BluetoothManager getBluetoothManager(Context context) {
        return (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
    }

    public static BluetoothAdapter getBluetoothAdapter(Context context) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1)
            return BluetoothAdapter.getDefaultAdapter();
        else {
            return getBluetoothManager(context).getAdapter();
        }
    }

    public static Set<BluetoothDevice> getBondedDevices(Context context) {
        BluetoothAdapter bluetoothAdapter = null;
        Set<BluetoothDevice> bondedDevices = null;
        if (context == null) {
            Log.d(TAG, " the param is null ,return");
            return null;
        }
        bluetoothAdapter = getBluetoothAdapter(context);
        if (context.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, " the permission: BLUETOOTH_CONNECT is not granted ,return");
            return null;
        }
        bondedDevices = bluetoothAdapter.getBondedDevices();

        for (BluetoothDevice device : bondedDevices) {
            Log.d(TAG, "the bonded device name: " + device.getName() + "  the mac address: " + device.getAddress());
        }
        return bondedDevices;
    }

    public static boolean checkDeviceIsBonded(Context context, String address) {
        // boolean result = false;
        if (context == null || address == null) {
            Log.d(TAG, " check bonded state, the param is null ,return false");
            return false;
        }
        Set<BluetoothDevice> bondedDevices = getBondedDevices(context);
        if (bondedDevices == null) {
            Log.d(TAG, " check bonded state, bondedDevices is null ,return false");
            return false;
        }
        Log.d(TAG, " the device address which should be connected: " + address);
        for (BluetoothDevice device : bondedDevices) {
            if (address.equals(device.getAddress())) {
                Log.d(TAG, " check bonded state, the device: " + address + "  is bonded,return true");
                return true;
            }
        }
        Log.d(TAG, " check bonded state, not found");
        return false;
    }

    public static BluetoothDevice getBondedBtDevice(Context context, String address) {
        // boolean result = false;
        if (context == null || address == null) {
            Log.d(TAG, " get bounded device, the param is null ,return false");
            return null;
        }
        Set<BluetoothDevice> bondedDevices = getBondedDevices(context);
        if (bondedDevices == null) {
            Log.d(TAG, " get bounded device, bondedDevices is null ,return false");
            return null;
        }

        for (BluetoothDevice device : bondedDevices) {
            if (address.equals(device.getAddress())) {
                Log.d(TAG, " get bounded device, the device: " + address + "  is bonded,return device");
                return device;
            }
        }
        Log.d(TAG, " get bounded device, not found");
        return null;
    }


    public static boolean unBondedBleDevice(Context context, String address) {
        boolean result;
        BluetoothDevice device;
        Method removeBondDevice;
        if (context == null || address == null) {
            Log.d(TAG, " unBondedBleDevice,the device is null,unbond fail");
            return false;
        }

        if (context.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, " the permission: BLUETOOTH_CONNECT is not granted ,return");
            return false;
        }

        device = getBondedBtDevice(context, address);
        if (device == null) {
            Log.d(TAG, " the device is null, not bonded");
            return true;
        }

        if (device.getBondState() == BluetoothDevice.BOND_NONE) {
            Log.d(TAG, " the device state is not bonded");
            return true;
        }
        try {
            removeBondDevice = device.getClass().getMethod("removeBond");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        try {
            result = (boolean) removeBondDevice.invoke(device);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        Log.d(TAG, " the device unbonded result: " + result);
        return result;
    }

}
